package com.cuitrip.business.location.map;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.amap.api.services.core.PoiItem;
import com.amap.api.services.poisearch.PoiItemDetail;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;
import com.cuitrip.service.R;

import java.util.ArrayList;

/**
 * Created by MiJack on 2015/12/13.
 */
public class MapListFragment extends Fragment {
    MapAdapter mapAdapter;
    GaoDeMapActivity mainActivity;
    ListView listView;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof GaoDeMapActivity) {
            mainActivity = (GaoDeMapActivity) activity;
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mainActivity = null;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.simple_list, container, false);
        listView = (ListView) view.findViewById(R.id.listview);
        mapAdapter = new MapAdapter();
        listView.setAdapter(mapAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                PoiItem item = (PoiItem) view.getTag();
                mainActivity.selectPoiItem(item);
            }
        });
        return view;
    }

    public void search(String search) {
        PoiSearch.Query query = new PoiSearch.Query(search, "", "");
        query.setPageSize(30);
        PoiSearch poiSearch = new PoiSearch(getActivity(), query);
        poiSearch.setOnPoiSearchListener(new PoiSearch.OnPoiSearchListener() {
            @Override
            public void onPoiSearched(PoiResult poiResult, int i) {
//                aMap.getMapScreenMarkers().clear();
                if (poiResult == null || poiResult.getPois() == null || poiResult.getPois().isEmpty()) {
//                    toastError();
                    return;
                }
                mapAdapter.setDatas(poiResult.getPois());
//                LogHelper.e("search", "onPoiSearched " + i + " |" + poiResult.getPois().size());
//                placePop.showPriceType(container, poiResult.getPois());
            }

            @Override
            public void onPoiItemDetailSearched(PoiItemDetail poiItemDetail, int i) {

            }

        });
        poiSearch.searchPOIAsyn();
    }

    class MapAdapter extends BaseAdapter {

        private ArrayList<PoiItem> datas;

        @Override
        public int getCount() {
            return datas == null ? 0 : datas.size();
        }

        @Override
        public PoiItem getItem(int position) {
            return datas.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(parent.getContext()).inflate(android.R.layout.simple_list_item_1, parent, false);
            }
            PoiItem item = getItem(position);
            ((TextView) convertView).setText(item.getTitle());
            convertView.setTag(item);
            return convertView;
        }

        public void setDatas(ArrayList<PoiItem> datas) {
            this.datas = datas;
            this.notifyDataSetChanged();
        }
    }
}
